﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using log4net;
using VT.Dialogs;


namespace SHKurzy.Upsize
{
	/// <summary>
	/// Interaction logic for UpsizeWin.xaml
	/// </summary>
	public partial class UpsizeWin : Window
	{
		private static readonly ILog log = LogManager.GetLogger(typeof(UpsizeWin));

		public UpsizeWin()
		{
			InitializeComponent();
		}

		private void button1_Click(object sender, RoutedEventArgs e)
		{
			try
			{
				MakeUpsize(@"D:\tmp\svh\svh_pokus.mdb", "");
			}
			catch(Exception ex)
			{
				VTDialog.ShowException("Chyba při upsize", ex, log);
		//        log.Error("Upsize exception!");
		//        log.Error(string.Format("    message:'{0}'", ex.Message));
		////		log.Error(string.Format("    inner ex:{0}", ex.));


				//MessageBox.Show(ex.Message, "", MessageBoxButton.OK, MessageBoxImage.Error);
			}
			//OleDbConnection con;   // create connection
			//OleDbCommand com;  // create command
			//OleDbDataReader dr;  //Dataread for read data from database
			//con = new OleDbConnection(connectionStr);
			//com = new OleDbCommand("Select * from Kurzy", con);
			//con.Open();                                        // open the coinnection
			//dr = com.ExecuteReader();




		//    // Get the data table containing the schema
		////	var dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
		//    //These few lines of code can give you all system tables schema now if you want to have only tables created by the user then you have to put a filter on data of your DataTable dt.

		//    foreach (DataRow row in dt.Rows)
		//    {
		//        //string strSheetTableName = row["TABLE_NAME"].ToString();
		//        //if (row["TABLE_TYPE"].ToString()=="TABLE")
		//        //                    ddlTableName.Items.Add(strTableName);
		//        //                i++;


		//        Title += row["TABLE_NAME"].ToString() + "-" + row["TABLE_TYPE"].ToString();

		//        log.Info(row["TABLE_NAME"].ToString() + "-" + row["TABLE_TYPE"].ToString());

		//    }
		}

		private void MakeUpsize(string msAccessDatabaseFile, string sqliteDatabaseFile)
		{
			var upsizeHelper = new UpsizeHelper(msAccessDatabaseFile);

			var tables = upsizeHelper.GetTableList();
			var pozadovaneTabulky = "dokumenty,JménaAdresy,Korespondence,Kurzy,Kurzy staré,Oslovení".Split(',');

			var chybejiciTabulky = from p in tables
			                       where !pozadovaneTabulky.Contains(p)
			                       select p;

			if (chybejiciTabulky.Any())
			{
				throw new Exception(string.Format("V MS Access databázi '{2}' neexistují tabulky: {0} !{1}Nelze pokračovat.", 
					                              string.Join(",", chybejiciTabulky), Environment.NewLine, msAccessDatabaseFile));
			}

			//  povinne tabulky tedy existuji, takze je zkusim vsechny nacist do pameti
			var tableDokumenty = GetMSAccessTable(upsizeHelper, "dokumenty");
			var tableJmenaAdresy = GetMSAccessTable(upsizeHelper, "JménaAdresy");
			var tableKorespondence = GetMSAccessTable(upsizeHelper, "Korespondence");
			var tableKurzy = GetMSAccessTable(upsizeHelper, "Kurzy");
			var tableKurzyStare = GetMSAccessTable(upsizeHelper, "Kurzy staré");
			var tableOsloveni = GetMSAccessTable(upsizeHelper, "Oslovení");
			

		}

		private DataTable GetMSAccessTable(UpsizeHelper upsizeHelper, string tableName)
		{
			var result = upsizeHelper.GetTable(tableName);
			if (result != null)
			{
				log.Info(string.Format("Read MS Access table: {0} rows:{1} columns:{2}", tableName, result.Rows.Count, result.Columns.Count));
			}
			else
			{
				log.Info(string.Format("Read MS Access table: {0} - returner NULL ?", tableName));
			}
			return result;
		}
	}
}
